import 'package:flutter/material.dart';

/// context扩展
extension SlcRouterHelper on BuildContext {

  ///
  Future<T?> push<T extends Object?>(Route<T> route) {
    return Navigator.of(this).push(route);
  }

  /**
   *
   */
  Future<dynamic> pushPage(Widget page, {RouteSettings? settings}) {
    return push(MaterialPageRoute(builder: (context) => page, settings: settings));
  }

  /**
   *
   */
  Future<T?> pushReplacement<T extends Object?, TO extends Object?>(Route<T> newRoute, {TO? result}) {
    return Navigator.of(this).pushReplacement(newRoute, result: result);
  }

  /**
   *
   */
  Future<dynamic> pushReplacementPage<TO extends Object?>(Widget page,
      {RouteSettings? settings, TO? result}) {
    return pushReplacement(MaterialPageRoute(builder: (context) => page, settings: settings),
        result: result);
  }

  /**
   *
   */
  Future<dynamic> pushNamed(String routeName,
      {Map<String, dynamic> arguments = const <String, dynamic>{}, Object? data, int requestCode = 0}) {
    return pushNamedInfo(
        SlcRouterInfo.fromInfo(routeName, arguments: arguments, data: data, requestCode: requestCode));
  }

  /**
   *
   */
  Future<dynamic> pushNamedInfo(SlcRouterInfo routerInfo) {
    return Navigator.of(this).pushNamed(routerInfo.routeName, arguments: routerInfo);
  }

  /**
   *
   */
  Future<dynamic> pushReplacementNamed(String routeName,
      {Map<String, dynamic> arguments = const <String, dynamic>{}, Object? data, int requestCode = 0}) {
    return pushReplacementNamedInfo(
        SlcRouterInfo.fromInfo(routeName, arguments: arguments, data: data, requestCode: requestCode));
  }

  /**
   *
   */
  Future<dynamic> pushReplacementNamedInfo(SlcRouterInfo routerInfo) {
    return Navigator.of(this).pushReplacementNamed(routerInfo.routeName, arguments: routerInfo);
  }

  /**
   * 获取路由参数
   */
  SlcRouterInfo? getSlcRouterInfo({bool listen = true}) {
    Object? arguments = ModalRoute.settingsOf(this)?.arguments;
    if (arguments == null) {
      return null;
    }
    return arguments as SlcRouterInfo?;
  }
}

/// 路由查询草书类
/// 根据路由名称获取查询参数
class _QueryInfo {
  static _QueryInfo parseQueryInfo(String routeName) {
    Uri uri = Uri.parse(routeName);
    return _QueryInfo(uri.path, queryParameters: uri.queryParameters);
  }

  String routeName;

  Map<String, String> queryParameters;

  _QueryInfo(this.routeName, {this.queryParameters = const <String, String>{}});
}

///
/// 路由信息
///
class SlcRouterInfo {
  String routeName;
  Map<String, dynamic> arguments;
  Map<String, String> queryParameters;
  Object? data;
  int requestCode;

  SlcRouterInfo(this.routeName,
      {this.arguments = const <String, dynamic>{},
      this.queryParameters = const <String, String>{},
      this.data,
      this.requestCode = 0}) {}

  static SlcRouterInfo fromInfo(String routeName,
      {Map<String, dynamic> arguments = const <String, dynamic>{}, Object? data, int requestCode = 0}) {
    _QueryInfo queryInfo = _QueryInfo.parseQueryInfo(routeName);
    return SlcRouterInfo(queryInfo.routeName,
        arguments: arguments,
        queryParameters: queryInfo.queryParameters,
        data: data,
        requestCode: requestCode);
  }
}

class PushPageParams {
  Widget page;
  RouteSettings? routeSettings;
  int requestCode;

  PushPageParams(this.page, {this.routeSettings, this.requestCode = 0});
}
